<?php
class user_listController extends adminController{
    
    public function index() {
        //获取管理组列表
        $this->group_list=model('user_group')->group_list();
        //获取会员列表
        $this->show();  
    }
    //AJAX获取内容列表
    public function ajax_content_list()
    {
        //分页参数
        $listRows=20;
        $page = intval($_POST['page']);
        $limit_start = ($page - 1) * $listRows;
        $limit = $limit_start . ',' . $listRows;

        $where=array();

        //状态
        $status=intval($_POST['status']);
        switch ($status) {
            case '1':
                $where[]=' AND A.status=1';
                break;
            case '2':
                $where[]=' AND A.status=0';
                break;
        }

        //搜索
        $search=in(urldecode($_POST['keyword']));
        if(!is_utf8($search)){
            $search=auto_charset($search);
        }
        if(!empty($search)){
        $where[]=' AND (A.username like "%' . $search . '%" OR A.nicename like "%' . $search . '%" OR A.tel like "%' . $search . '%"  OR A.company like "%' . $search . '%"  OR A.qq like "%' . $search . '%" )';
        }

        //推荐位
        $gid=intval($_POST['gid']);
        if(!empty($gid)){
            $where[]=' AND B.gid='.$gid;
        }

        $str ='';
        if(!empty($where)){
            foreach ($where as $value) {
                $str .= $value;
            }
        }

        //列表信息
        $list=model('user_list')->user_list($limit,$str);
        $count=model('user_list')->count($str);

        //总页数
        $countpage=ceil($count/$listRows);

        if(!empty($list)){
            foreach ($list as $vo) {
                $html.='
                <tr>
                    <td>'.$vo['uid'].'</td>
                    <td>'.$vo['username'].'</td>
                    <td>'.$vo['nicename'].'</td>
                    <td>'.$vo['gname'].'</td><td>';
                    if($vo['status']){
                        $html.='启用';
                    }else{
                        $html.='禁用';
                    }
                $html.='</td><td>
                <a class="opentab operating" href="'.url('user_list/edit',array('uid' => $vo['uid'])).'" ><span class="icon">&#xf040;</span> 修改</a>
                <a class="operating" href="javascript:vouid(0);" onClick="del(\''.url('user_list/del').'\',\''.$vo['uid'].'\',this)"><span class="icon">&#xf014;</span> 删除</a>
                </td>
                </tr>
                ';
            }
            $status=1;
        }else{
            $html='<tr><td colspan="6"><center>暂无查找到内容<center></td></tr>';
            $status=0;
        }

        @header("Content-type:text/html");
        echo json_encode(array('status' => $status, 'info' => $html ,'countpage'=>$countpage));
    }

    //获取地区
    public function get_province() {
        $pid=intval($_POST['pid']);
        $type=intval($_POST['type']);
        $list=model('region')->region_list($pid,$type);
        $this->msg($list);
    }

    //添加
    public function add() {
        $this->action_name='添加';
        $this->action='add';
        $this->user_group=model('user_group')->group_list();
        $this->province_list=model('region')->region_list(1,1);
        //获取tabhtml
        $tab_html=apiHook('hook_user_info_tab_html');
        $this->tab_html=hook_merge($tab_html);

        $this->show('user_list/info'); 
    }

    public function add_save() {
        //录入模型处理
        if($_POST['password']<>$_POST['password2']){
            $this->msg('两次密码输入不同！',0);
            return;
        }
        if(model('user_list')->user_info($_POST['username'])){
            $this->msg('帐号不能重复！',0);
            return;
        }
        $_POST['password']=md5($_POST['password']);
        model('user_list')->add($_POST);
        apiHook('hook_user_info_save',$_POST);
        $this->msg('会员添加成功！',1);
    }

    //修改
    public function edit() {
        $uid=$_GET['uid'];
        $this->alert_str($uid,'int');
        $this->info=model('user_list')->info($uid);
        $this->action_name='编辑';
        $this->action='edit';
        //获取tabhtml
        $tab_html=apiHook('hook_user_info_tab_html',$this->info);
        $this->tab_html=hook_merge($tab_html);

        $this->user_group=model('user_group')->group_list();
        $this->info_group=model('user_group')->info($this->info['gid']);
        $this->province_list=model('region')->region_list(1,1);
        $this->city_list=model('region')->region_list($this->info['province'],2);
        $this->show('user_list/info'); 
    }

    public function edit_save() {
        $uid=$_POST['uid'];
        $this->alert_str($uid,'int',true);
        //录入模型处理
        if (!empty($_POST['password']))
        {
            if (empty($_POST['password2']))
            {
               $this->msg('未填写确认密码！',0);
               return;
            }
            if($_POST['password']<>$_POST['password2']){
                $this->msg('两次密码输入不同！',0);
                return;
            }
            if(model('user_list')->user_info($_POST['username'],$_POST['id'])){
                $this->msg('帐号不能重复！',0);
                return;
            }
            $_POST['password']=md5($_POST['password']);
        }else{
            unset($_POST['password']);
        }
        model('user_list')->edit($_POST);
        apiHook('hook_user_info_save',$_POST);
        $this->msg('会员修改成功! ',1);
    }

    //删除
    public function del() {
        $uid=intval($_POST['uid']);
        $this->alert_str($uid,'int',true); 
        //录入模型处理
        $info=model('user_list')->info($uid);
        model('user_list')->del($uid);
        apiHook('hook_user_info_del',$gid);
        $this->msg('会员删除成功！',1);
    }


}